package com.xiaol.springbootredis.web;

import com.xiaol.springbootredis.common.wrapper.WrapMapper;
import com.xiaol.springbootredis.common.wrapper.Wrapper;
import com.xiaol.springbootredis.model.domain.User;
import com.xiaol.springbootredis.security.jjwt.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;

@Slf4j
@Api(value = "User - UserController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestMapping(path = "user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
public class UserController {
    @ApiOperation(httpMethod = "POST", value = "")
    @PostMapping("/login")
    public Wrapper add(User user) throws ServletException {
        String name = user.getUserName();

        String pass = user.getPassWord();
        if (!"admin".equals(name)) {
            throw new ServletException("no such user");
        }
        if (!"1234".equals(pass)) {
            throw new ServletException("wrong password");
        }

        return WrapMapper.ok(JwtUtil.getToken(name));
    }
}
